﻿using System;
using System.Collections.Generic;
using System.Linq;
using BLL;
using Logic.BusinessLogic.SocialNetworks;

namespace BO
{
    public partial class MasterAccount
    {
        private long? totalUserCount;

        public long TotalUserCount
        {
            get
            {
                if (!totalUserCount.HasValue)
                {
                    if (this.Accounts == null)
                        LoadAccounts();

                    totalUserCount = this.Accounts.Sum(a => a.TwFollowersNum + a.FbPageFanNum);
                }
                return totalUserCount.Value;
            }
        }

        private void LoadAccounts()
        {
            this.Accounts = Logic.BusinessLogic.AccountService.GetAccountsByMasterID(this.ID);
        }

        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }

        public AccountOverview OverView { get; set; }
    }

    public partial class Account
    {
        public AccountOverview OverView { get; set; }

        public bool HasTwitter
        {
            get { return !string.IsNullOrEmpty(this.TwToken) && !string.IsNullOrEmpty(this.TwSecret); }
        }

        public bool HasFacebook
        {
            get { return !string.IsNullOrEmpty(this.FbToken); }
        }

        public bool HasYoutube
        {
            get { return !string.IsNullOrEmpty(this.YtToken); }
        }

        public bool HasWordpress
        {
            get
            {
                return !string.IsNullOrEmpty(this.WpUsername) && !string.IsNullOrEmpty(this.WpPassword) && !string.IsNullOrEmpty(this.WpUrl);
            }
        }

		public string FbPublishingToken
		{
			get 
			{
				if (!string.IsNullOrEmpty(this.FbPageToken))
					return FbPageToken;
				return FbToken;
			}
		}

        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }

        public static Account GetWithMaster(int id)
        {
            return BLL.Accounts.Get()
                                .Where(Accounts.Columns.ID, Actions.Equal, id)
                                .Join(BLL.MasterAccounts.Get(), BLL.Accounts.Columns.MasterAccountID)
                                .SelectOne();
        }

        public SnSelector GetAvailableNetworks()
        {
            var sn = new SnSelector();
            sn.IsTwitter = this.HasTwitter;
            sn.IsFacebook = this.HasFacebook;
            sn.IsYoutube = this.HasYoutube;

            return sn;
        }
    }

    public partial class Post
    {
        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }

        protected override void OnPreInsert()
        {
            base.OnPreInsert();
            this.EntityStatus = EntityStatuses.Active;
            if (this.TimePosted == DateTime.MinValue)
                this.TimePosted = DateTime.Now;
        }

        public PostTypes PostType
        {
            get { return (PostTypes)this.EnumPostType; }
            set { this.EnumPostType = (int)value; }
        }

        public enum PostTypes
        {
            StatusLine = 1,
            Link = 2,
            BlogPost = 3,
            Video = 4,
            Photo = 5
        }

        public void UpdateWithNewValues(BO.PostStat todayStat)
        {
            this.FbCommentNum = todayStat.FbCommentNum;
            this.FbLikeNum = todayStat.FbLikeNum;
            this.FbShareNum = todayStat.FbShareNum;
            this.TwReplyNum = todayStat.TwReplyNum;
            this.TwRetweetNum = todayStat.TwRetweetNum;
            this.WpCommentNum = todayStat.WpCommentNum;
            this.YtCommentnum = todayStat.YtCommentnum;
            this.YtRating = todayStat.YtRating;
            this.YtViewsNum = todayStat.YtViewsNum;

            this.Update();
        }
    }

    public partial class UnProcessedPost
    {
        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }

        public ProcessStatuses ProcessStatus
        {
            get { return (ProcessStatuses)this.EnumStage; }
            set { this.EnumStage = (int)value; }
        }

        public enum ProcessStatuses
        {
            Unprocessed,
            Accepted,
            Rejected,
            Done
        }

        protected override void OnPreInsert()
        {
            base.OnPreInsert();
            this.EntityStatus = EntityStatuses.Active;
        }

        public void SetSnSelection(SnSelector snSelector)
        {
            this.PostOnFb = snSelector.IsFacebook;
            this.PostOnTw = snSelector.IsTwitter;
        }
    }

    public partial class TracedAccount
    {
        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }

        public AccountTypes AccountType
        {
            get { return (AccountTypes)this.EnumType; }
            set { this.EnumType = (int)value; }
        }

        public enum AccountTypes
        {
            None = 0,
            Facebook = 1,
            Twitter = 2
        }
    }

    public partial class PostStat
    {
        [EntityStatus("StatusEnum")]
        public EntityStatuses EntityStatus
        {
            get { return (EntityStatuses)this.StatusEnum; }
            set { StatusEnum = (int)value; }
        }
    }

    public partial class AccountStat
    {
        //[EntityStatus("StatusEnum")]
        //public EntityStatuses EntityStatus
        //{
        //    get { return (EntityStatuses)this.StatusEnum; }
        //    set { StatusEnum = (int)value; }
        //}
    }

    public class AccountOverview
    {
        #region Posts and Comments

        public int TotalPostCount { get; set; }

        public int FbPostCount { get; set; }

        public int TwPostCount { get; set; }

        public long FbCommentCount { get; set; }

        public long TwReplyCount { get; set; }

        public long TwRetweetCount { get; set; }

        public int MyProperty { get; set; }

        public long TotalCommentCount
        {
            get { return FbCommentCount + TwReplyCount; }
        }

        public double AvgComments
        {
            get
            {
                if (TotalPostCount == 0)
                    return 0;
                return TotalCommentCount / TotalPostCount;
            }
        }

        public double FbAvgComments
        {
            get
            {
                if (TotalPostCount == 0)
                    return 0;
                return FbCommentCount / TotalPostCount;
            }
        }

        public double TwAvgReplies
        {
            get
            {
                if (TotalPostCount == 0)
                    return 0;
                return TwReplyCount / TotalPostCount;
            }
        }

        public AccountOverview() { }

        public AccountOverview(List<BO.Post> posts)
        {
            if (posts == null)
                return;
            TotalPostCount = posts.Count;
            FbPostCount = posts.Count(p => !string.IsNullOrEmpty(p.FbItemID));
            TwPostCount = posts.Count(p => !string.IsNullOrEmpty(p.TwStatusId));
            FbCommentCount = posts.Sum(p => p.FbCommentNum);
            TwReplyCount = posts.Sum(p => p.TwReplyNum);
            TwRetweetCount = posts.Sum(p => p.TwRetweetNum);
        }

        #endregion Posts and Comments

        public int TotalFanCount { get { return FbFanCount + TwFollowerCount; } }

        public int FbFanCount { get; set; }

        public int TwFollowerCount { get; set; }
    }
}